# 公用自定义工具类
from flask import session, current_app, g
import functools
from info.models import User


def do_index_class(index):
    """自定义过滤器，过滤点击排序html的class"""
    if index == 0:
        return "first"
    elif index == 1:
        return "second"
    elif index == 2:
        return "third"
    else:
        return ""

def user_login_data(f):
    # 使用functools.wraps去装饰内存函数,可以保持当前装饰器
    # 去装饰的函数的__name__的值不遍
    @functools.wraps(f)
    def wrapper(*args,**kwargs):
        user_id = session.get("user_id", None)
        user = None
        # 尝试查询用户模型
        if user_id:
            try:
                user = User.query.get(user_id)
            except Exception as e:
                current_app.logger.error(e)

        g.user = user

        return f(*args,**kwargs)
    return wrapper



    # 查询用户登录信息
    # def query_user_data():
    #     user_id = session.get("user_id", None)
    #     user = None
    #     # 尝试查询用户模型
    #     if user_id:
    #         try:
    #             user = User.query.get(user_id)
    #         except Exception as e:
    #             current_app.logger.error(e)
    #         return user
    #     return None
